// pages/test-resources/test-resources.js - 临时测试页面
const config = require('../../utils/config.js');
const { getIconUrl, getImageUrl } = require('../../utils/imageUtils.js');

Page({
  data: {
    // 测试用的图标和图片URLs
    testResources: [
      {
        name: '发送图标',
        url: getIconUrl('send.png'),
        type: 'icon'
      },
      {
        name: 'AI头像',
        url: getIconUrl('AIPortrait.png'),
        type: 'icon'
      },
      {
        name: '默认头像',
        url: getImageUrl('default-avatar.png'),
        type: 'image'
      },
      {
        name: '聊天图标',
        url: getIconUrl('chat.png'),
        type: 'icon'
      },
      {
        name: '微信二维码',
        url: getIconUrl('weixin.png'),
        type: 'icon'
      }
    ],
    
    // 测试结果
    testResults: [],
    
    // API测试
    apiBaseUrl: config.API_BASE_URL
  },

  onLoad() {
    console.log('API Base URL:', config.API_BASE_URL);
    console.log('Resource Config:', config.RESOURCE_CONFIG);
    
    this.testResources();
    this.testApiConnection();
  },

  /**
   * 测试图片资源加载
   */
  testResources() {
    const results = this.data.testResources.map(resource => {
      return {
        ...resource,
        status: 'testing',
        error: null
      };
    });
    
    this.setData({ testResults: results });
    
    // 测试每个资源
    results.forEach((resource, index) => {
      wx.downloadFile({
        url: resource.url,
        success: () => {
          this.updateTestResult(index, 'success', null);
        },
        fail: (err) => {
          this.updateTestResult(index, 'failed', err.errMsg);
        }
      });
    });
  },

  /**
   * 更新测试结果
   */
  updateTestResult(index, status, error) {
    const results = [...this.data.testResults];
    results[index] = {
      ...results[index],
      status: status,
      error: error
    };
    this.setData({ testResults: results });
  },

  /**
   * 测试API连接
   */
  async testApiConnection() {
    try {
      const response = await wx.request({
        url: config.API_BASE_URL + '/health',
        method: 'GET'
      });
      
      console.log('API连接测试结果:', response);
    } catch (error) {
      console.error('API连接测试失败:', error);
    }
  },

  /**
   * 复制URL到剪贴板
   */
  copyUrl(e) {
    const url = e.currentTarget.dataset.url;
    wx.setClipboardData({
      data: url,
      success() {
        wx.showToast({
          title: '已复制到剪贴板',
          icon: 'success'
        });
      }
    });
  }
});
